package com.cencat.order.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单实体类
 * @author cencat
 * @description 固定商家场景多租户SaaS企业综合管理系统订单实体
 * @since 2024-01-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_order")
@Schema(name = "Order", description = "订单实体")
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "订单ID")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    @Schema(description = "租户ID")
    @TableField("tenant_id")
    @NotNull(message = "租户ID不能为空")
    private Long tenantId;

    @Schema(description = "订单编号")
    @TableField("order_no")
    @NotBlank(message = "订单编号不能为空")
    @Size(max = 64, message = "订单编号长度不能超过64个字符")
    private String orderNo;

    @Schema(description = "商家ID")
    @TableField("merchant_id")
    @NotNull(message = "商家ID不能为空")
    private Long merchantId;

    @Schema(description = "商家名称")
    @TableField("merchant_name")
    @NotBlank(message = "商家名称不能为空")
    @Size(max = 100, message = "商家名称长度不能超过100个字符")
    private String merchantName;

    @Schema(description = "用户ID")
    @TableField("user_id")
    @NotNull(message = "用户ID不能为空")
    private Long userId;

    @Schema(description = "用户名称")
    @TableField("user_name")
    @NotBlank(message = "用户名称不能为空")
    @Size(max = 50, message = "用户名称长度不能超过50个字符")
    private String userName;

    @Schema(description = "用户手机号")
    @TableField("user_phone")
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
    private String userPhone;

    @Schema(description = "订单类型：PRODUCT-商品订单，SERVICE-服务订单")
    @TableField("order_type")
    @NotBlank(message = "订单类型不能为空")
    @Pattern(regexp = "^(PRODUCT|SERVICE)$", message = "订单类型只能是PRODUCT或SERVICE")
    private String orderType;

    @Schema(description = "订单状态：PENDING-待支付，PAID-已支付，SHIPPED-已发货，DELIVERED-已送达，COMPLETED-已完成，CANCELLED-已取消，REFUNDED-已退款")
    @TableField("order_status")
    @NotBlank(message = "订单状态不能为空")
    @Pattern(regexp = "^(PENDING|PAID|SHIPPED|DELIVERED|COMPLETED|CANCELLED|REFUNDED)$", message = "订单状态不正确")
    private String orderStatus;

    @Schema(description = "支付状态：UNPAID-未支付，PAID-已支付，REFUNDED-已退款，PARTIAL_REFUNDED-部分退款")
    @TableField("payment_status")
    @NotBlank(message = "支付状态不能为空")
    @Pattern(regexp = "^(UNPAID|PAID|REFUNDED|PARTIAL_REFUNDED)$", message = "支付状态不正确")
    private String paymentStatus;

    @Schema(description = "物流状态：PENDING-待发货，SHIPPED-已发货，DELIVERED-已送达")
    @TableField("shipping_status")
    @Pattern(regexp = "^(PENDING|SHIPPED|DELIVERED)$", message = "物流状态不正确")
    private String shippingStatus;

    @Schema(description = "配送方式")
    @TableField("shipping_method")
    @Size(max = 50, message = "配送方式长度不能超过50个字符")
    private String shippingMethod;

    @Schema(description = "物流公司")
    @TableField("shipping_company")
    @Size(max = 100, message = "物流公司长度不能超过100个字符")
    private String shippingCompany;

    @Schema(description = "物流单号")
    @TableField("shipping_no")
    @Size(max = 100, message = "物流单号长度不能超过100个字符")
    private String shippingNo;

    @Schema(description = "订单总金额")
    @TableField("total_amount")
    @NotNull(message = "订单总金额不能为空")
    @DecimalMin(value = "0.01", message = "订单总金额必须大于0")
    @Digits(integer = 10, fraction = 2, message = "订单总金额格式不正确")
    private BigDecimal totalAmount;

    @Schema(description = "实付金额")
    @TableField("paid_amount")
    @DecimalMin(value = "0.00", message = "实付金额不能为负数")
    @Digits(integer = 10, fraction = 2, message = "实付金额格式不正确")
    private BigDecimal paidAmount;

    @Schema(description = "优惠金额")
    @TableField("discount_amount")
    @DecimalMin(value = "0.00", message = "优惠金额不能为负数")
    @Digits(integer = 10, fraction = 2, message = "优惠金额格式不正确")
    private BigDecimal discountAmount;

    @Schema(description = "运费")
    @TableField("shipping_fee")
    @DecimalMin(value = "0.00", message = "运费不能为负数")
    @Digits(integer = 10, fraction = 2, message = "运费格式不正确")
    private BigDecimal shippingFee;

    @Schema(description = "应付金额")
    @TableField("payable_amount")
    @DecimalMin(value = "0.01", message = "应付金额必须大于0")
    @Digits(integer = 10, fraction = 2, message = "应付金额格式不正确")
    private BigDecimal payableAmount;

    @Schema(description = "支付单号")
    @TableField("payment_no")
    @Size(max = 100, message = "支付单号长度不能超过100个字符")
    private String paymentNo;

    @Schema(description = "开始时间")
    @TableField("start_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    @Schema(description = "结束时间")
    @TableField("end_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    @Schema(description = "发货时间（别名）")
    @TableField(exist = false)
    private LocalDateTime shippingTime;

    @Schema(description = "支付方式：ALIPAY-支付宝，WECHAT-微信支付，BANK-银行卡，CASH-现金")
    @TableField("payment_method")
    @Pattern(regexp = "^(ALIPAY|WECHAT|BANK|CASH)$", message = "支付方式不正确")
    private String paymentMethod;

    @Schema(description = "支付时间")
    @TableField("payment_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime paymentTime;

    @Schema(description = "收货地址")
    @TableField("delivery_address")
    @Size(max = 500, message = "收货地址长度不能超过500个字符")
    private String deliveryAddress;

    @Schema(description = "收货人姓名")
    @TableField("receiver_name")
    @Size(max = 50, message = "收货人姓名长度不能超过50个字符")
    private String receiverName;

    @Schema(description = "收货人手机号")
    @TableField("receiver_phone")
    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "收货人手机号格式不正确")
    private String receiverPhone;

    @Schema(description = "发货时间")
    @TableField("ship_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime shipTime;

    @Schema(description = "送达时间")
    @TableField("delivery_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime deliveryTime;

    @Schema(description = "完成时间")
    @TableField("complete_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime completeTime;

    @Schema(description = "完成时间（别名）")
    @TableField(exist = false)
    private LocalDateTime completedTime;

    @Schema(description = "取消时间")
    @TableField("cancel_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime cancelTime;

    @Schema(description = "取消原因")
    @TableField("cancel_reason")
    @Size(max = 500, message = "取消原因长度不能超过500个字符")
    private String cancelReason;

    @Schema(description = "订单备注")
    @TableField("remark")
    @Size(max = 1000, message = "订单备注长度不能超过1000个字符")
    private String remark;

    @Schema(description = "订单来源：WEB-网页，APP-手机应用，MINI-小程序，API-接口")
    @TableField("order_source")
    @Pattern(regexp = "^(WEB|APP|MINI|API)$", message = "订单来源不正确")
    private String orderSource;

    @Schema(description = "是否删除：0-未删除，1-已删除")
    @TableField("is_deleted")
    @TableLogic
    private Integer isDeleted;

    @Schema(description = "创建时间")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "创建人ID")
    @TableField("create_by")
    private Long createBy;

    @Schema(description = "更新人ID")
    @TableField("update_by")
    private Long updateBy;

    @Schema(description = "版本号")
    @TableField("version")
    @Version
    private Integer version;
}